<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Messageuser;
use Illuminate\Http\Request;

class MessageuserController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'messageuser';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-房型数据管理';
    }

    public function index(Request $request){
        $page = $request->get('page', 1);
        $search = $request->all();
        $this->_response['_active']['_action']      = 'list';

        $messageuserModel = new Messageuser();

        $data = $messageuserModel->getData($search, $page, 20);

        $this->_response['messageuser'] = $data;

        $this->_response['messageuser']['pageData']['page'] = $page;
        $this->_response['messageuser']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['messageuser']['pageData']['lastPage'] , '/admin/messageuser?');
        //return $this->_response;
        return view('admin.messageuser.list', $this->_response);
    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.messageuser.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Messageuser $request){
        $data = $request->all();
        $saveData = [
            'address' => $data['address'],
            'room' => $data['room'],
            'area' => $data['area'],
            'phone' => $data['phone'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $messageuserModel = new Messageuser();
        $user=$messageuserModel->saveData($saveData);
        return $user;

//        if($messageuserModel->saveData($saveData)){
//            return response()->json(['code' => 0, 'msg' => '添加成功']);
//        }else{
//            return response()->json(['code' => -1, 'msg' => '添加失败']);
//        }

    }

    public function edit($id){
        $this->_response['messageuser'] = Messageuser::where('id', $id)->first();

        return view('admin.messageuser.edit', $this->_response);
    }


    public function doedit(\App\Http\Requests\Messageuser $request){
        $data = $request->all();
        $saveData = [
            'address' => $data['address'],
            'room' => $data['room'],
            'area' => $data['area'],
            'phone' => $data['phone'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];

        $messageuserModel = new Messageuser();
        if($messageuserModel->setData($data['id'],$saveData)){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
            return redirect('/admin/messageuser');
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }


    public function delete($id){
        $messageuserModel = new Messageuser();
        $data=[
            'deleted_at'=>date('Y-m-d H:i:s',time())
        ];
        if($messageuserModel->delData($id,$data)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
            return redirect('/admin/banner');
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }



}